package com.niit.controller;

import com.niit.pojo.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @program: springcloud
 * @description:
 * @author: XZQ
 * @create: 2019-12-02 11:59
 **/
@RestController
@RequestMapping("/consumer")
public class DeptConsumerController {

    //消费者 不应该有service层
    //RESTTemplate 供我们调用

    @Autowired
    private RestTemplate template;

    private static final String REST_URL_PREFIX = "http://localhost:8001";

    @RequestMapping("/dept/get/{id}")
    public Dept get(@PathVariable("id") Long id) {
        return template.getForObject(REST_URL_PREFIX + "/dept/get/" + id, Dept.class);
    }

    @RequestMapping("/dept/add")
    public boolean add(@RequestBody Dept dept) {
        return template.postForObject(REST_URL_PREFIX + "/dept/add", dept, Boolean.class);
    }

    @RequestMapping("/dept/list")
    public List<Dept> list() {
        return template.getForObject(REST_URL_PREFIX + "/dept/list", List.class);
    }

    @RequestMapping("/dept/delete/{id}")
    public void delete(@PathVariable Long id) {
        template.delete(REST_URL_PREFIX + "/dept/delete/" + id);
    }
}
